﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KKU.RBSFPP.Shared.DAC;

namespace KKU.RBSFPP.Web.Administrator.Controllers
{
    public class FactorController : Controller
    {
        private RBSFPPModelContainer db = new RBSFPPModelContainer();

        //
        // GET: /Factor/

        public ActionResult Index()
        {
            var now = DateTime.Now;
            var factorvalues = db.FactorValues.Include("Factor").OrderByDescending(it => it.CreateOn).Take(4);
                               
                                   
                                   //where (it.CreateOn.Day == now.Day)&&(it.FactorId == 1)                            
                               //select it;

                
            

            ViewBag.LastUpdateOn =  factorvalues.First().CreateOn;

            return View(factorvalues.ToList());
        }

        //
        // GET: /Factor/Details/5

        public ActionResult Details(int id = 0)
        {
            FactorValue factorvalue = db.FactorValues.Single(f => f.FactorValueId == id);
            if (factorvalue == null)
            {
                return HttpNotFound();
            }
            return View(factorvalue);
        }

        //
        // GET: /Factor/Create

        public ActionResult Create()
        {
            ViewBag.FactorId = new SelectList(db.Factors, "FactorId", "FactorName");
            return View();
        }

        //
        // POST: /Factor/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(FactorValue factorvalue)
        {
            if (ModelState.IsValid)
            {
                db.FactorValues.AddObject(factorvalue);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.FactorId = new SelectList(db.Factors, "FactorId", "FactorName", factorvalue.FactorId);
            return View(factorvalue);
        }

        //
        // GET: /Factor/Edit/5
        

        public ActionResult Update()
        {
            var qry = from it in db.FactorValues.OrderByDescending(it => it.CreateOn).Take(4)
                      select it;

            return View(qry);
        }

        [HttpPost]
        public ActionResult Update(string Factor11, string Factor12, string Factor13, string Factor14)
        {
            bool facetor01ValidationResult = false;
            
            double Factor01;
            if (double.TryParse(Factor11, out Factor01) == false)
            {
                ModelState.AddModelError("Factor11", "ค่าปัจจัยจะต้องเป็นตัวเลขเท่านั้น");
            }
            else if (Factor01 <= 0)
            {
                ModelState.AddModelError("Factor11", "ค่าปัจจัยจะต้องมากกว่า 0 ขึ้นไปเท่านั้น");
            }
            else
            {
                facetor01ValidationResult = true;
            }


            bool facetor02ValidationResult = false;
            double Factor02;
            if (double.TryParse(Factor12,out Factor02) == false)
            {
                ModelState.AddModelError("Factor12", "ค่าปัจจัยจะต้องเป็นตัวเลขเท่านั้น");
            }
             else if (Factor02 <= 0)
            {
                ModelState.AddModelError("Factor12", "ค่าปัจจัยจะต้องมากกว่า 0 ขึ้นไปเท่านั้น");
            }
            else
            {
                facetor02ValidationResult = true;
            }


            bool facetor03ValidationResult = false;
            double Factor03;
            if (double.TryParse(Factor13, out Factor03) == false)
            {
                ModelState.AddModelError("Factor13", "ค่าปัจจัยจะต้องเป็นตัวเลขเท่านั้น");
            }
            else if (Factor03 <= 0)
            {
                ModelState.AddModelError("Factor13", "ค่าปัจจัยจะต้องมากกว่า 0 ขึ้นไปเท่านั้น");
            }
            else
            {
                facetor03ValidationResult = true;
            }



            bool facetor04ValidationResult = false;
            double Factor04;
            if (double.TryParse(Factor14, out Factor04) == false)
            {
                ModelState.AddModelError("Factor14", "ค่าปัจจัยจะต้องเป็นตัวเลขเท่านั้น");
            }
            else if (Factor04 <= 0)
            {
                ModelState.AddModelError("Factor14", "ค่าปัจจัยจะต้องมากกว่า 0 ขึ้นไปเท่านั้น");
            }
            else
            {
                facetor04ValidationResult = true;
            }


            if (facetor01ValidationResult && facetor02ValidationResult && facetor03ValidationResult && facetor04ValidationResult)
            {

                var now = DateTime.Now;
                var factors = new double[] { Factor01, Factor02, Factor03, Factor04 };
                for (int index = 0; index < 4; index++)
                {
                    var newfactor = new FactorValue
                    {
                        FactorId = index + 1,
                        CreateOn = now,
                        Value = factors[index]
                    };
                    db.FactorValues.AddObject(newfactor);
                }
                db.SaveChanges();

                return Redirect("Index");
            }

            var qry = from it in db.FactorValues.OrderByDescending(it => it.CreateOn).Take(4)
                      select it;
            return View(qry);
            //return View(qry);
        }


        public ActionResult Edit(int id = 0)
        {
            FactorValue factorvalue = db.FactorValues.Single(f => f.FactorValueId == id);
            if (factorvalue == null)
            {
                return HttpNotFound();
            }
            ViewBag.FactorId = new SelectList(db.Factors, "FactorId", "FactorName", factorvalue.FactorId);
            return View(factorvalue);
        }

        //
        // POST: /Factor/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(FactorValue factorvalue)
        {
            if (ModelState.IsValid)
            {
                db.FactorValues.Attach(factorvalue);
                db.ObjectStateManager.ChangeObjectState(factorvalue, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.FactorId = new SelectList(db.Factors, "FactorId", "FactorName", factorvalue.FactorId);
            return View(factorvalue);
        }

        //
        // GET: /Factor/Delete/5

        public ActionResult Delete(int id = 0)
        {
            FactorValue factorvalue = db.FactorValues.Single(f => f.FactorValueId == id);
            if (factorvalue == null)
            {
                return HttpNotFound();
            }
            return View(factorvalue);
        }

        //
        // POST: /Factor/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            FactorValue factorvalue = db.FactorValues.Single(f => f.FactorValueId == id);
            db.FactorValues.DeleteObject(factorvalue);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        public bool facetor02ValidationResult { get; set; }
    }
}